<script type="text/javascript">
jQuery(function(){
	//置顶开始
	//置顶按钮显示/隐藏实现
    $(window).scroll(function(){
        var scroll_top = $(document).scrollTop();//滚动条到顶部的垂直高度
        if(scroll_top >= 100){
              $("#goto-top").fadeIn(300);
          }else{
              $("#goto-top").fadeOut(300);
        }
    });
    //置顶
    $("#goto-top").click(function(e){
        e.preventDefault();
        $(document.documentElement).animate({
            scrollTop: 0
            },600);
        //支持chrome
        $(document.body).animate({
            scrollTop: 0
            },600);
     });
	//置顶结束		
});


function praise(articleId){
  	if(articleId!=null && articleId!=undefined && articleId!=""){  	
  		//cookie 记录点击记录，一天只能赞一次	
  		if($.cookie('praise'+articleId)==undefined){
	  		$.ajax({
		  		"url":"/article/article_praise_ajax",
		  		"type":"POST",
		  		"data":{"articleId":articleId},
		  		"dataType":"text",
		  		"success":function(ret){
		  			if(ret=="1"){
		  				$("#id_up_count_"+articleId).html(parseInt($("#id_up_count_"+articleId).html())+1);
		  				$.cookie('praise'+articleId, true, { expires: 1, path: '/' });
		  			}
		  		}
	  		});
  		}else{
  			alert("已被赞过！");
  		}

  	}
}

function isEmail(str){
    var reg = /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/;
    if(reg.test(str)){
      return true;
    }else{
      return false;
    }
}

function isURL(str){
	var strRegex = '^((https|http|ftp|rtsp|mms)?://)' 
			+ '?(([0-9a-z_!~*\'().&=+$%-]+: )?[0-9a-z_!~*\'().&=+$%-]+@)?' //ftp的user@ 
			+ '(([0-9]{1,3}.){3}[0-9]{1,3}' // IP形式的URL- 199.194.52.184 
			+ '|' // 允许IP和DOMAIN（域名） 
			+ '([0-9a-z_!~*\'()-]+.)*' // 域名- www. 
			+ '([0-9a-z][0-9a-z-]{0,61})?[0-9a-z].' // 二级域名 
			+ '[a-z]{2,6})' // first level domain- .com or .museum 
			+ '(:[0-9]{1,4})?' // 端口- :80 
			+ '((/?)|' // a slash isn't required if there is no file name 
			+ '(/[0-9a-z_!~*\'().;?:@&=+$,%#-]+)+/?)$'; 

	var re=new RegExp(strRegex); 						
	if (re.test(str)) { 
		return true; 
	} else { 
		return false; 
	} 
}

// 建议的正则
function isURL2(str){
    return !!str.match(/(((^https?:(?:\/\/)?)(?:[-;:&=\+\$,\w]+@)?[A-Za-z0-9.-]+|(?:www.|[-;:&=\+\$,\w]+@)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%@.\w_]*)#?(?:[\w]*))?)$/g);
}


function checkFriendLinkForm(){
	var id_name = $("#id_name");
	var id_link = $("#id_link");
	var id_intro = $("#id_intro");
	var id_vcode = $("#id_vcode");
	var id_vcode_src = $("#id_vcode_src");
		
	var id_msg =  $("#id_msg");


	id_msg.html("");

	if(id_name.val()==""){
	  id_name.focus();
	  id_msg.html("请填写博客名！");
	  return false;
	}

	if(id_link.val()==""){
	  id_link.focus();
	  id_msg.html("请填写首页链接");
	  return false;
	}

	if(!isURL(id_link.val())){
	  id_link.focus();
	  id_msg.html("首页链接格式有误！");
	  return false;
	}

	if(id_intro.val()==""){
	  id_intro.focus();
	  id_msg.html("请填写博客介绍!");
	  return false;
	}

	if(id_vcode.val()==""){
	  id_vcode.focus();
	  id_msg.html("请填写验证码!");
	  return false;
	}

	var vcodeLen = id_vcode.val().length;
	if(vcodeLen!=6){
		id_msg.html("验证码要求6位！");
		return false;
	}

	

	$.ajax({
		"url":"/friendlink/add",
		"type":"POST",
		"data":{"name":id_name.val(),"link":id_link.val(),"intro":id_intro.val(),"vcode":id_vcode.val(),"vcodeSrc":id_vcode_src.val()},
		"dataType":"json",
		"success":function(ret){
			if(ret.code=="1"){
				id_name.val("");
				id_link.val("http://");
				id_vcode.val("");
				id_intro.val("");


				$("#captcha_image").attr({"src":"/captcha/"+ret.captchaId+".png"});
				id_vcode_src.val(ret.captchaId);

				id_msg.html("您的博客已经添加成功，正等待博主发现！");
				setTimeout(function(){
					id_msg.html("");
				},6000);
				

			}else if(ret.code=="2"){	
				$("#captcha_image").attr({"src":"/captcha/"+ret.captchaId+".png"});
				id_vcode_src.val(ret.captchaId);

				id_msg.html("验证码有误！");	

			setTimeout(function(){
					id_msg.html("");
				},5000);
			}else if(ret.code=="3"){	
				$("#captcha_image").attr({"src":"/captcha/"+ret.captchaId+".png"});
				id_vcode_src.val(ret.captchaId);

				id_msg.html("已添加过您的博客！");	

			setTimeout(function(){
					id_msg.html("");
				},5000);
			}else{
				$("#captcha_image").attr({"src":"/captcha/"+ret.captchaId+".png"});
				id_vcode_src.val(ret.captchaId);

				id_msg.html("发生异常，操作失效！");											
			setTimeout(function(){
					id_msg.html("");
				},5000);
			}
		}
	});

	return true;
}


function passFriendLink(id,action){
	$.ajax({
		"url":"/friendlink/pass",
		"type":"POST",
		"data":{"id":id,"action":action},
		"dataType":"json",
		"success":function(ret){
			if(ret.code=="1"){
				alert("操作成功！")
			}else{
			alert("操作失败！")	
			}
		}
	});
}


function checkCommentForm(){
	var id_email = $("#id_email");
	var id_nickName = $("#id_nickName");
	var id_content = $("#id_content");
	var id_vcode = $("#id_vcode");
	var id_vcode_src=$("#id_vcode_src");
	var id_articleId=$("#id_articleId");
	var id_userId = $("#id_userId");
	var id_replyer = $("#id_replyer");
	var id_commentId = $("#id_commentId");

	var id_blog_title=$("#id_blog_title a").html();

	var id_msg =  $("#id_msg");
	var ret_html="";
	id_msg.html("处理中...");

	if(id_nickName.val()==""){
	  id_nickName.focus();
	  id_msg.html("请填写您的称呼！");
	  return false;
	}

	if(id_email.val()==""){
	  id_email.focus();
	  id_msg.html("您的邮箱会被保密，仅用回复用途！");
	  return false;
	}

	if(!isEmail(id_email.val())){
	  id_email.focus();
	  id_msg.html("您的邮箱格式有误！");
	  return false;
	}

	if(id_content.val()==""){
	  id_content.focus();
	  id_msg.html("写点什么吧!");
	  return false;
	}

	if(id_vcode.val()==""){
	  id_vcode.focus();
	  id_msg.html("请填写验证码!");
	  return false;
	}

	var vcodeLen = id_vcode.val().length;
	if(vcodeLen!=6){
		id_msg.html("验证码要求6位！");
		return false;
	}	

	if(AntiSqlValid(id_content.val())){
		 id_msg.html("抱歉，内容包含非法字符！");
	  	return false;
	}

	$.ajax({
		"url":"/add_comment_ajax",
		"type":"POST",
		"data":{"userId":id_userId.val(),"userName":id_nickName.val(),"articleId":id_articleId.val(),"email":id_email.val(),"replyer":id_replyer.val(),"content":id_content.val(),"vcode":id_vcode.val(),"vcodeSrc":id_vcode_src.val(),"blogTile":id_blog_title,"commentId":id_commentId.val()},
		"dataType":"json",
		"success":function(ret){
			if(ret.code=="1"){
				var commentCount = parseInt($("#id_comment_count").val())+1;

				ret_html="<div id='items"+ret.commentId+"' class='items'><p><b>"+commentCount+" 楼：</b>"+id_nickName.val()+" 刚刚发表 <a href='javascript:;' onclick=\"reply_action('"+id_nickName.val()+"','"+ret.commentId+"')\">回复 TA</a></p><div class='item'>"+ret.content+"</div></div>";
				$(".comment_content").append(ret_html);				

				$("#id_comment_count").val(commentCount);
				$("#id_comment_count2").html(commentCount);
				
				id_vcode.val("");
				id_content.val("");
				id_content.attr({"placeholder":""})

				id_replyer.val("");
				id_commentId.val("");

				$(".no_comment").remove();

				$("#captcha_image").attr({"src":"/captcha/"+ret.captchaId+".png"});
				id_vcode_src.val(ret.captchaId);

				id_msg.html("谢谢您的点评！");
				setTimeout(function(){
					id_msg.html("");
				},5000);
			}else if(ret.code=="2"){	
				$("#captcha_image").attr({"src":"/captcha/"+ret.captchaId+".png"});
				id_vcode_src.val(ret.captchaId);

				id_msg.html("验证码有误！");	

				setTimeout(function(){
					id_msg.html("");
				},5000);
			}else{
				$("#captcha_image").attr({"src":"/captcha/"+ret.captchaId+".png"});
				id_vcode_src.val(ret.captchaId);

				id_msg.html("发生异常，操作失效！");											
				setTimeout(function(){
					id_msg.html("");
				},5000);
			}
		}
	});

	return true;
}

function reply_action(userName,commentId){
	$("#id_replyer").val(userName);
	$("#id_commentId").val(commentId);	
	$("#id_content").attr({"placeholder":"@"+userName});
}


function del_comment_action(commentId){
	if(confirm("是否确定删除？")){
		$.ajax({
			"url":"/del_comment_ajax",
			"type":"POST",
			"data":{"commentId":commentId},
			"dataType":"json",
			"success":function(ret){
				if(ret=="1"){
					var commentCount = parseInt($("#id_comment_count").val())-1;
					$("#id_comment_count").val(commentCount);
					$("#id_comment_count2").html(commentCount);

					$("#items"+commentId).remove();					
				}else if(ret=="2"){	
					alert("抱歉，您无法删除此评论！");
				}
			}
		});

	}
}

function checkAboutMeForm(){
    var id_email = $("#id_email");
    var id_nickName = $("#id_nickName");
    var id_content = $("#id_content");
    var id_vcode = $("#id_vcode");			  	

    var id_msg =  $("#id_msg");					
    id_msg.html("");

    if(id_nickName.val()==""){
      id_nickName.focus();
      id_msg.html("请填写您的称呼！");
      return false;
    }

    if(id_email.val()==""){
      id_email.focus();
      id_msg.html("邮箱会被保密，仅回复用途！");
      return false;
    }

    if(!isEmail(id_email.val())){
      id_email.focus();
      id_msg.html("您的邮箱格式有误！");
      return false;
    }
    
    if(id_content.val()==""){
      id_content.focus();
      id_msg.html("写点什么吧!");
      return false;
    }

    if(id_vcode.val()==""){
      id_vcode.focus();
      id_msg.html("请填写验证码!");
      return false;
    }

    var vcodeLen = id_vcode.val().length;
    if(vcodeLen!=6){
  		id_msg.html("验证码要求6位！");
  		return false;
  	}

    return true;
}

function reply_mymsg_action(userName,msgId){
	$("#id_replyId").val(msgId);	
	$("#id_content").attr({"placeholder":"@"+userName});
}


function setSrcQuery(e, q) {
	  var src  = e.src;
	  var p = src.indexOf('?');
	  if (p >= 0) {
	    src = src.substr(0, p);
	  }
	  e.src = src + "?" + q
  }

  function playAudio() {
	  var le = document.getElementById("lang");
	  var lang = le.options[le.selectedIndex].value;
	  var e = document.getElementById('audio')
	  setSrcQuery(e, "lang=" + lang)
	  e.style.display = 'block';
	  e.autoplay = 'true';
	  return false;
	}

function changeLang() {
	  var e = document.getElementById('audio')
	  if (e.style.display == 'block') {
	    playAudio();
	  }
	}

function reload() {
	  setSrcQuery(document.getElementById('captcha_image'), "reload=" + (new Date()).getTime());
	  //setSrcQuery(document.getElementById('audio'), (new Date()).getTime());
	  return false;
}

//禁止包含SQL语句
function AntiSqlValid(text)
{
     re=/select|update|delete|truncate|join|union|exec|insert|drop|count|script|>|<|%/i;
     return re.test(text);
 } 

</script>